package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.ImsVehMerUnit;
import com.ruoyi.system.mapper.ImsVehMerUnitMapper;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.IImsVehMerUnitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 商户单位Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-07-10
 */
@Service
public class ImsVehMerUnitServiceImpl implements IImsVehMerUnitService
{
    @Autowired
    private ImsVehMerUnitMapper imsVehCusUnitMapper;

    @Autowired
    private SysDeptMapper sysDeptMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询客户单位
     * 
     * @param id 客户单位主键
     * @return 客户单位
     */
    @Override
    public ImsVehMerUnit selectImsVehCusUnitById(Long id)
    {
        return imsVehCusUnitMapper.selectImsVehCusUnitById(id);
    }

    @Override
    public ImsVehMerUnit selectImsVehCusUnitByDeptId(Long deptId) {
        return imsVehCusUnitMapper.selectImsVehCusUnitByDeptId(deptId);
    }

    /**
     * 查询客户单位列表
     * 
     * @param imsVehCusUnit 客户单位
     * @return 客户单位
     */
    @Override
    public List<ImsVehMerUnit> selectImsVehCusUnitList(ImsVehMerUnit imsVehCusUnit)
    {
        if (imsVehCusUnit.getDeptId() == null) {
            SysUser loginUser = SecurityUtils.getLoginUser().getUser();
            imsVehCusUnit.setDeptId(loginUser.getDeptId());
        }
        return imsVehCusUnitMapper.selectImsVehCusUnitList(imsVehCusUnit);
    }
;
    /**
     * 新增客户单位
     * 
     * @param imsVehCusUnit 客户单位
     * @return 结果
     */
    @Override
    public int insertImsVehCusUnit(ImsVehMerUnit imsVehCusUnit)
    {
        return imsVehCusUnitMapper.insertImsVehCusUnit(imsVehCusUnit);
    }

    /**
     * 修改客户单位
     * 
     * @param imsVehCusUnit 客户单位
     * @return 结果
     */
    @Override
    public int updateImsVehCusUnit(ImsVehMerUnit imsVehCusUnit)
    {
        imsVehCusUnit.setCompanyCertified(1);
        int i = imsVehCusUnitMapper.updateImsVehCusUnit(imsVehCusUnit);
        if (imsVehCusUnit.getDeptId() != null) {
            SysDept dept = new SysDept();
            dept.setDeptId(imsVehCusUnit.getDeptId());
            dept.setDeptName(imsVehCusUnit.getCustomerUnitName());
            sysDeptMapper.updateDept(dept);

            SysUser sysUser = new SysUser();
            sysUser.setDeptId(imsVehCusUnit.getDeptId());
            List<SysUser> sysUsers = sysUserMapper.selectUserList(sysUser);
            if (sysUsers != null && !sysUsers.isEmpty()) {
                for (SysUser user : sysUsers) {
                    if (user != null) {
                        user.setCompanyCertified(1);
                        sysUserMapper.updateUser(user);
                    }
                }
            }
        }
        return i;
    }

    /**
     * 批量删除客户单位
     * 
     * @param ids 需要删除的客户单位主键
     * @return 结果
     */
    @Override
    public int deleteImsVehCusUnitByIds(Long[] ids)
    {
        return imsVehCusUnitMapper.deleteImsVehCusUnitByIds(ids);
    }

    /**
     * 删除客户单位信息
     * 
     * @param id 客户单位主键
     * @return 结果
     */
    @Override
    public int deleteImsVehCusUnitById(Long id)
    {
        return imsVehCusUnitMapper.deleteImsVehCusUnitById(id);
    }
}
